package com.dy.lock.screen.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.dy.lock.screen.constants.Constant;
import com.dy.lock.screen.dao.OrderDao;
import com.dy.lock.screen.dao.ScoreDao;
import com.dy.lock.screen.pojo.GoodsInfo;
import com.dy.lock.screen.pojo.OrderInfo;
import com.dy.lock.screen.pojo.ScoreInfo;
import com.dy.lock.screen.scoket.Head;
import com.dy.lock.screen.service.GoodsService;
import com.dy.lock.screen.service.OrderService;
import com.dy.lock.screen.utils.CommonUtil;

@Service("orderService")
public class OrderServiceImpl implements OrderService {

	@Resource(name = "orderDao")
	private OrderDao orderDao;
	@Resource(name = "scoreDao")
	private ScoreDao scoreDao;
	@Resource(name = "goodsService")
	private GoodsService goodsService;

	@Override
	public int update(OrderInfo orderInfo, Head head) {
		return this.orderDao.update(orderInfo);
	}

	@Override
	public List<OrderInfo> queryPage(Map<String, Object> para, Head head) {
		List<OrderInfo> list = this.orderDao.queryT(para);
		return list;
	}

	@Override
	public int save(OrderInfo orderInfo, GoodsInfo goodsIfno, int surplus,
			Head head) {
		ScoreInfo sc = new ScoreInfo();
		sc.setCustomerId(orderInfo.getCustomerId());
		sc.setExchangedScore(surplus);
		sc.setUpdateTime(new Date());
		GoodsInfo gi = new GoodsInfo();
		gi.setGoodsId(goodsIfno.getGoodsId());
		Integer stock = goodsIfno.getGoodsStock();
		if (stock > 0) {
			Integer stockTemp = CommonUtil.sub(stock, 1);
			gi.setGoodsStock(stockTemp);
			if (stockTemp == 0) {
				gi.setGoodsState(Constant.GOODS_OFF_LINE);
			}
			gi.setUpdateTime(new Date());
			this.goodsService.update(gi);
		}
		this.scoreDao.updateScore(sc);
		return this.orderDao.insert(orderInfo);
	}

	@Override
	public OrderInfo queryByPrimaryKey(String orderId) {
		return this.orderDao.queryByPrimaryKey(orderId);

	}

	@Override
	public OrderInfo getOrderDetail(String orderId, String userId) {
		OrderInfo orderInfo = new OrderInfo();
		orderInfo.setOrderId(orderId);
		orderInfo.setUserId(userId);
		return this.orderDao.queryOrderDetail(orderInfo);
	}

}
